﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tasker.Composition;
using Tasker.Infrastructure.Entities;
using Tasker.Infrastructure.Services;

namespace Tasker.WebUI.Controllers
{
    public abstract class TaskerControllerBase : Controller
    {
        private IAccountService _account = Resolver.Resolve<IAccountService>();



        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            IUser user = _account.SelectUser(CurrentUserLogin);

            ViewBag.DisplayUserNick = DisplayUserLogin;
            ViewBag.DisplayUserName = DisplayUserName;

            ViewBag.CurrentUserNick = CurrentUserLogin;
            ViewBag.CurrentUserPosition = user.Position;
            ViewBag.CurrentUserFullName = FullName(user.FirstName, user.LastName, user.Login);
            ViewBag.CurrentUserComment = user.Comment;

            ViewBag.PicturePath = Tasker.Common.UrlHelper.GetUserPicturePath(user.PictureName, user.PictureType);

            ViewBag.Signed = _account.SignedUser != null;
            base.OnActionExecuting(filterContext);
        }

        protected string FullName(string FirstName, string LastName, string Login)
        {
            if(FirstName == null && LastName == null)
                return Login;
            return FirstName + " " + LastName;
        }

        protected string DisplayUserLogin
        {
            get
            {

                if (RouteData.Values["login"] == null)
                    return string.Empty;
                if (RouteData.Values["login"].ToString() == "my")
                    return CurrentUserLogin;
                return RouteData.Values["login"].ToString();
            }
        }
        protected string DisplayUserName
        {
            get
            {
                IUser user = _account.SelectUser(DisplayUserLogin);
                if (user != null)
                    return user.FirstName;
                return "";
            }
        }

        protected int CurrentUserId
        {
            get
            {
                if (_account.SignedUser != null)
                    return _account.SignedUser.Id;
                else
                    return -1;
            }
        }

        protected string CurrentUserLogin
        {
            get
            {
                if (_account.SignedUser != null)
                    return _account.SignedUser.Login;
                else
                    return string.Empty;
            }
        }

        protected IUser SignedUser
        {
            get { return _account.SignedUser; }
        }

        public string UserFullName(IUser user)
        {
            if (user.FirstName == null && user.LastName == null)
                return user.Login;
            return user.FirstName + " " + user.LastName;
        }

    }
}
